[Java] 資料傳輸

這學期在計算機網路這門課寫了一個C++Socket遊戲,
於是趁著寒假空檔摸索JavaSocket,也寫了一個點對點的傳輸軟體,
有興趣的可以玩玩看

DataTransfer.jar

但似乎傳輸圖片檔打不開,有空再研究看看問題在哪裡 …

Code

Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package datatransfer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame Frame = new JFrame();
Frame.setSize(300,300);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
Frame.setLayout(null);
JButton Button_Server = new JButton("接收");
JButton Button_Client = new JButton("傳送");
Button_Server.setSize(60,25);
Button_Client.setSize(60,25);
Button_Server.setLocation(50, 115);
Button_Client.setLocation(170, 115);
JPanel Panel = new JPanel();
Panel.setLayout(null);
Panel.setSize(300,300);
Panel.add(Button_Server);
Panel.add(Button_Client);
Frame.add(Panel);
Button_Server.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Frame.remove(Panel);
Server server = new Server();
Frame.add(server);
Frame.repaint();
}
});
Button_Client.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Frame.remove(Panel);
Client client = new Client();
Frame.add(client);
Frame.repaint();
}
});
}
}
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package datatransfer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Client extends JPanel{
Client(){
this.setSize(300,300);
this.setLayout(null);
JButton Button_Choose = new JButton("選擇");
JButton Button_Send = new JButton("傳送");
JTextField DataSite = new JTextField();
JTextField IP = new JTextField();
Button_Choose.setSize(60,25);
Button_Send.setSize(60,25);
DataSite.setSize(200,25);
IP.setSize(200,25);
Button_Choose.setLocation(215, 100);
Button_Send.setLocation(215, 135);
DataSite.setLocation(10, 100);
IP.setLocation(10, 135);
DataSite.setText("請選擇檔案");
IP.setText("請輸入欲傳送之IP位置");
this.add(Button_Choose);
this.add(Button_Send);
this.add(DataSite);
this.add(IP);
Button_Choose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
JFileChooser FileChooser =new JFileChooser();
int ret = FileChooser.showOpenDialog(null);
if( ret == JFileChooser.APPROVE_OPTION)
{
DataSite.setText(FileChooser.getCurrentDirectory()+"\\"+FileChooser.getSelectedFile().getName());
}
} catch (Exception e2){
JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
Button_Send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
File file = new File(DataSite.getText());
Socket Socket_Server = new Socket(IP.getText(), 9999);
JOptionPane.showMessageDialog(null,"開始傳送檔案!");
PrintStream printStream =
new PrintStream(Socket_Server.getOutputStream());
printStream.println(file.getName());
BufferedInputStream inputStream =
new BufferedInputStream(
new FileInputStream(file));
int readin;
while((readin = inputStream.read()) != -1) {
printStream.write(readin);
Thread.yield();
}
printStream.flush();
printStream.close();
inputStream.close();
Socket_Server.close();
JOptionPane.showMessageDialog(null,"傳送完成!");
}
catch(Exception e2) {
;
}
}
});
}
}
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package datatransfer;
import java.io.*;
import java.net.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class Server extends JPanel{
ServerSocket Socket_Server ;
JLabel Label_Waiting ;
Server(){
this.setSize(300,300);
this.setLayout(null);
String IP;
try {
IP = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
IP = "Error" ;
}
JLabel Label_IP = new JLabel("您的IP為:"+IP) ;
Label_IP.setSize(300,100);
Label_IP.setLocation(65, 50);
Label_Waiting = new JLabel("等候連結中...") ;
Label_Waiting.setSize(300,100);
Label_Waiting.setLocation(100, 90);
this.add(Label_IP) ;
this.add(Label_Waiting) ;
Timer timer = new Timer();
timer.schedule(new ListenConnection(), 100 , 100);
}
class ListenConnection extends TimerTask{
public void run() {
try {
Socket_Server = new ServerSocket(9999);
Socket Socket_Client = Socket_Server.accept();
String DataName = new BufferedReader(new InputStreamReader(Socket_Client.getInputStream())).readLine();
Label_Waiting.setText("接收檔案中..");
BufferedInputStream Stream_Input = new BufferedInputStream(Socket_Client.getInputStream());
BufferedOutputStream Stream_Output = new BufferedOutputStream(new FileOutputStream(DataName));
int ReadIn;
while((ReadIn = Stream_Input.read()) != -1) {
Stream_Output.write(ReadIn);
Thread.yield();
}
Stream_Output.flush();
Stream_Output.close();
Stream_Input.close();
Socket_Client.close();
Label_Waiting.setText("接收完成!");
} catch (IOException e) {
//JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}